import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.Skills;
import com.speljohan.rsbot.script.wrappers.RSCharacter;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class GarrettsFiremaker extends Script implements ServerMessageListener,
		PaintListener {

	// GUI
	private javax.swing.JFrame frame;

	private javax.swing.JButton jButton1;

	private javax.swing.JCheckBox jCheckBox1;

	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JComboBox jComboBox2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JTextField jTextField1;
	// OTHER VARIABLES
	private long scriptStartTime;
	private String lastMessage;
	private String checkMessage;

	private int logType;
	private int failCount = 0;
	private int space = 0;
	private int fireCount = 0;
	private int startFireXP = 0;
	private int startLvl = 0;
	private int runEnergy = random(35, 65);
	private boolean getNewSpot = true;
	private boolean ready = false;
	private boolean setupSkill = false;
	private boolean fog = false;
	private boolean HOVER = false;
	private boolean openMenu = true;
	private RSTile[] thePath;
	private RSTile[] theBank;
	private RSTile[] theStart;
	private int randTile;
	private String openStr;
	// ITEM ID
	private final int tinderBox = 590;
	private final int normalLog = 1511;
	private final int oakLog = 1521;

	private final int willowLog = 1519;
	private final int mapleLog = 1517;
	private final int yewLog = 1515;
	private final int magicLog = 1513;
	// TILES
	private final RSTile Draynor[] = { new RSTile(3097, 3247),
			new RSTile(3097, 3248), new RSTile(3097, 3249),
			new RSTile(3097, 3250) };
	private final RSTile DraynorBank[] = { new RSTile(3091, 3245) };
	private final RSTile DraynorPath[] = { new RSTile(3079, 3249),
			new RSTile(3088, 3248) };

	private final RSTile Varrock[] = { new RSTile(3199, 3431),
			new RSTile(3201, 3428), new RSTile(3201, 3429),
			new RSTile(3201, 3430) };
	private final RSTile VarrockBank[] = { new RSTile(3190, 3435) };
	private final RSTile VarrockPath[] = { new RSTile(3175, 3429),
			new RSTile(3186, 3430) };

	private final RSTile VarrockNorth[] = { new RSTile(3194, 3447),
			new RSTile(3194, 3448), new RSTile(3191, 3449),
			new RSTile(3191, 3450), new RSTile(3191, 3451) };
	private final RSTile VarrockNorthBank[] = { new RSTile(3181, 3444) };
	private final RSTile VarrockNorthPath[] = { new RSTile(3171, 3450),
			new RSTile(3181, 3450) };

	private final RSTile VarrockEast[] = { new RSTile(3263, 3429),
			new RSTile(3263, 3428), new RSTile(3263, 3430) };
	private final RSTile VarrockEastBank[] = { new RSTile(3254, 3419),
			new RSTile(3253, 3419) };
	private final RSTile VarrockEastPath[] = { new RSTile(3237, 3429),
			new RSTile(3247, 3429), new RSTile(3254, 3428) };

	private final RSTile GE1[] = { new RSTile(3180, 3488),
			new RSTile(3180, 3489), new RSTile(3180, 3490),
			new RSTile(3180, 3491) };
	private final RSTile GEBank1[] = { new RSTile(3166, 3489) };
	private final RSTile GE1Path[] = { new RSTile(3174, 3489) };

	private final RSTile GE2[] = { new RSTile(3178, 3496),
			new RSTile(3178, 3497) };
	private final RSTile GEBank2[] = { new RSTile(3166, 3490) };
	private final RSTile GE2Path[] = { new RSTile(3158, 3496),
			new RSTile(3196, 3495) };

	private final RSTile GE3[] = { new RSTile(3178, 3482),
			new RSTile(3178, 3483) };
	private final RSTile GEBank3[] = { new RSTile(3166, 3489) };
	private final RSTile GE3Path[] = { new RSTile(3158, 3483),
			new RSTile(3170, 3483) };

	private final RSTile Falador[] = { new RSTile(3024, 3359),
			new RSTile(3024, 3360), new RSTile(3024, 3361),
			new RSTile(3024, 3362), new RSTile(3024, 3363) };
	private final RSTile FaladorBank[] = { new RSTile(3013, 3354),
			new RSTile(3012, 3354) };
	private final RSTile FaladorPath[] = { new RSTile(2991, 3361),
			new RSTile(2999, 3362), new RSTile(3007, 3363),
			new RSTile(3014, 3359) };

	private final RSTile DuelArena[] = { new RSTile(3388, 3266),
			new RSTile(3388, 3265), new RSTile(3388, 3264) };
	private final RSTile DuelArenaBank[] = { new RSTile(3381, 3269),
			new RSTile(3382, 3270) };
	private final RSTile DuelArenaPath[] = { new RSTile(3361, 3264),
			new RSTile(3369, 3265), new RSTile(3379, 3265) };

	private final RSTile Yanille[] = { new RSTile(2607, 3097),
			new RSTile(2607, 3098), new RSTile(2608, 3099) };
	private final RSTile YanilleBank[] = { new RSTile(2614, 3094),
			new RSTile(2614, 3092), new RSTile(2614, 3091) };
	private final RSTile YanillePath[] = { new RSTile(2584, 3097),
			new RSTile(2592, 3097), new RSTile(2602, 3097),
			new RSTile(2607, 3093) };

	private final RSTile FOG[] = { new RSTile(1718, 5597),
			new RSTile(1718, 5598), new RSTile(1718, 5599),
			new RSTile(1718, 5600) };
	private final RSTile FOGPath[] = { new RSTile(1703, 5599) };

	public GarrettsFiremaker() {
		setParameter("author", "Garrett");
		setParameter("version", 1.11);
		setParameter("name", "Garrett's Firemaker");
		setParameter("category", "Firemaking");
	}

	private int antiBan() {
		final int random = random(1, 24);

		switch (random) {
		case 1:
			final int x = random(0, 750);
			final int y = random(0, 500);
			if (random(1, 10) == 5) {
				moveMouse(0, 0, x, y);
			}
			return random(1000, 1500);

		case 2:
			if (getCurrentTab() != Constants.TAB_INVENTORY) {
				openTab(Constants.TAB_INVENTORY);
				return random(500, 750);
			} else {
				return random(500, 750);
			}

		case 3:
			if (random(1, 40) == 30) {
				if (getMyPlayer().isMoving()) {
					return random(750, 1000);
				}
				if (getCurrentTab() != Constants.TAB_STATS) {
					openTab(Constants.TAB_STATS);
				}
				moveMouse(665, 355, 40, 20);
				wait(random(3000, 6000));
				return random(100, 200);
			}

		case 4:
			if (random(1, 3) == 2) {
				int angle = getCameraAngle() + random(-90, 90);
				if (angle < 0) {
					angle = 0;
				}
				if (angle > 359) {
					angle = 0;
				}

				setCameraRotation(angle);
				return random(500, 750);
			}
		}
		return 500;
	}

	private boolean burnLogs(final int log) {
		if (!bank.isOpen() && !getMyPlayer().isMoving()) {
			if (getInventoryCount(tinderBox) != 0
					&& getInventoryCount(log) != 0) {
				if (ready) {
					gatInventoryItem(log, "Use");
					ready = false;
				} else {
					guseItem(new RSItem(tinderBox, 1), new RSItem(log, 1));
				}
				wait(random(200, 400));
				while (true) {
					if (getInterface(740).isValid()) {
						clickContinue();
						ready = false;
						return true;
					}
					antiBan();
					try {
						if (checkMessage.contains("can't light")) {
							log("We cannot light here. Moving to a different spot.");
							checkMessage = null;
							getNewSpot = true;
							ready = false;
							return true;
						}
					} catch (final NullPointerException e) {
					}
					wait(random(100, 200));
					if (!ready) {
						gatInventoryItem(tinderBox, "Use");
						if (HOVER) {
							wait(random(100, 200));
							moveToInventoryItem(log);
						}
						ready = true;
					}
					try {
						if (checkMessage.contains("logs begin to burn")) {
							checkMessage = null;
							failCount = 0;
							fireCount++;
							return true;
						}
					} catch (final NullPointerException e) {
					}
					failCount++;
					if (failCount >= 100) {
						log("FAIL!");
						failCount = 0;
						ready = false;
						return true;
					}
				}
			} else {
				return false;
			}
		} else {
			bank.close();
			wait(random(500, 750));
			return true;
		}
	}

	private void cantPlace() {
		try {
			if (checkMessage.contains("can't light")) {
				log("We cannot light here. Moving to a different spot.");
				checkMessage = null;
				getNewSpot = true;
				ready = false;
				return;
			}
		} catch (final NullPointerException e) {
		}
	}

	public boolean clickTheCharacter(final RSCharacter c, final String action) {
		try {
			Point screenLoc = null;
			for (int i = 0; i < 20; i++) {
				screenLoc = c.getScreenLocation();
				if (!c.isValid() || !pointOnScreen(screenLoc)) {
					if (!getMyPlayer().isMoving()) {
						walkPath(thePath, false);
					}
					return false;
				}
				if (getMouseLocation().equals(screenLoc)) {
					break;
				}
				moveMouse(screenLoc);
			}
			screenLoc = c.getScreenLocation();
			if (!getMouseLocation().equals(screenLoc)) {
				return false;
			}
			final List<String> items = getMenuItems();
			if (items.size() <= 1) {
				return false;
			}
			if (items.get(0).toLowerCase().contains(action.toLowerCase())) {
				clickMouse(screenLoc, true);
				return true;
			} else {
				clickMouse(screenLoc, false);
				return atMenu(action);
			}
		} catch (final Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	private void doBank() {
		ready = false;
		if (getInventoryCount(logType) != 0) {
			bank.close();
			getNewSpot = true;
			wait(random(300, 500));
			return;
		}
		if (bank.depositAllExcept(tinderBox, logType)) {
			wait(random(300, 500));
		}
		if (bank.atItem(logType, "Withdraw-All")) {
			wait(random(750, 1000));
			failCount = 0;
			return;
		} else {
			wait(random(250, 500));
			failCount++;
			if (failCount >= 10) {
				bank.close();
				wait(random(750, 1000));
				stopAllScripts();
			} else {
				return;
			}
		}
	}

	private boolean energyCheck() {
		try {
			if (gEnergy() >= runEnergy && !isRunning()) {
				runEnergy = random(35, 65);
				return true;
			} else {
				return false;
			}
		} catch (final Exception e) {
			return false;
		}
	}

	public boolean gatInventoryItem(final int itemID, final String option) {
		if (getCurrentTab() != Constants.TAB_INVENTORY
				&& !RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()
				&& !RSInterface.getInterface(Constants.INTERFACE_STORE)
						.isValid()) {
			openTab(Constants.TAB_INVENTORY);
		}
		final int[] items = getInventoryArray();
		final java.util.List<Integer> possible = new ArrayList<Integer>();
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				possible.add(i);
			}
		}
		if (possible.size() == 0) {
			return false;
		}
		final int idx = possible.get(0);
		final Point t = getInventoryItemPoint(idx);
		clickMouse(t, 5, 5, true);
		return true;
	}

	private int gEnergy() {
		return Integer
				.parseInt(RSInterface.getChildInterface(750, 5).getText());
	}

	@Override
	public String getScriptDescription() {
		String html = "<html><head>";
		html += "</head><body>";
		html += "<center><strong><h2>" + getName() + " v" + getVersion()
				+ "</h2></strong></center>";
		html += "<center>Everything is in the GUI after you start the script.</center>";
		html += "</body></html>";
		return html;
	}

	public boolean guseItem(final RSItem item, final RSItem targetItem) {
		if (getCurrentTab() != Constants.TAB_INVENTORY) {
			openTab(Constants.TAB_INVENTORY);
		}
		gatInventoryItem(item.getID(), "Use");
		return gatInventoryItem(targetItem.getID(), "Use");
	}

	private void initComponents() {
		frame = new JFrame(getName() + " v" + getVersion());
		jButton1 = new javax.swing.JButton();
		jLabel3 = new javax.swing.JLabel();
		jLabel1 = new javax.swing.JLabel();
		jComboBox1 = new javax.swing.JComboBox();
		jTextField1 = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jComboBox2 = new javax.swing.JComboBox();
		jLabel4 = new javax.swing.JLabel();
		jCheckBox1 = new javax.swing.JCheckBox();

		jLabel2.setVisible(false);
		jTextField1.setVisible(false);

		frame
				.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jButton1.setText("Start Script");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
		jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel3.setText(getName() + " v" + getVersion());
		jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

		jLabel1.setText("Log Type:");

		jComboBox1
				.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
						"Normal", "Oak", "Willow", "Maple", "Yew", "Magic",
						"Custom" }));
		jComboBox1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(final java.awt.event.ActionEvent evt) {
				jComboBox1ActionPerformed(evt);
			}
		});

		jTextField1.setPreferredSize(new java.awt.Dimension(50, 20));

		jLabel2.setText("Log ID");

		jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Draynor Bank", "Duel Arena Bank", "Falador East Bank",
				"Fist of Guthix", "Grand Exchange 1", "Grand Exchange 2",
				"Grand Exchange 3", "Varrock East Bank",
				"Varrock West Bank (North)", "Varrock West Bank (South)",
				"Yanille Bank" }));

		jLabel4.setText("Location:");

		jCheckBox1.setText("Hover mouse over next log");

		final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				frame.getContentPane());
		frame.getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jLabel1))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jLabel3,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				293,
																				Short.MAX_VALUE))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jComboBox1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				jLabel2)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jTextField1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jLabel4))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jCheckBox1)
																						.addComponent(
																								jComboBox2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addContainerGap(
																				216,
																				Short.MAX_VALUE)
																		.addComponent(
																				jButton1)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jLabel3,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				29,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jLabel1)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jComboBox1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jLabel2)
																						.addComponent(
																								jTextField1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				104,
																				104,
																				104)
																		.addComponent(
																				jLabel4)))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jComboBox2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(jCheckBox1)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(jButton1)
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		frame.pack();
		frame.setVisible(true);
	}

	private void jButton1ActionPerformed(final java.awt.event.ActionEvent evt) {
		frame.setVisible(false);
		frame.dispose();
		openMenu = false;
	}

	private void jComboBox1ActionPerformed(final java.awt.event.ActionEvent evt) {
		if (jComboBox1.getSelectedItem() == "Custom") {
			jLabel2.setVisible(true);
			jTextField1.setVisible(true);
		} else {
			jLabel2.setVisible(false);
			jTextField1.setVisible(false);
		}
	}

	// *******************************************************//
	// MAIN LOOP
	// *******************************************************//
	@Override
	public int loop() {

		if (isLoggedIn() && !setupSkill) {
			startFireXP = skills.getCurrentSkillExp(Skills
					.getStatIndex("firemaking"));
			startLvl = skills.getCurrentSkillLevel(Skills
					.getStatIndex("firemaking"));
			setupSkill = true;
		}

		if (energyCheck()) {
			setRun(true);
			wait(random(750, 1000));
			return random(50, 150);
		}

		if (fog) {
			if (isLoggedIn() && !getMyPlayer().isMoving()) {
				makeFiresFOG(7605, thePath, theStart, randTile, openStr);
			}
			return random(50, 150);
		}

		if (isLoggedIn() && !getMyPlayer().isMoving()) {
			makeFires(theBank, thePath, theStart, randTile, openStr);
		}

		return random(50, 150);
	}

	private void makeFires(final RSTile[] banktile, final RSTile[] path,
			final RSTile[] start, final int rand, final String bankOpen) {
		cantPlace();
		if (getNewSpot) {
			ready = false;
			if (!walkTileMM(new RSTile(start[space].getX() - random(0, rand),
					start[space].getY()))) {
				walkPath(path, false);
				return;
			}
			getNewSpot = false;
			wait(random(1000, 1500));
			space++;
			if (space > start.length - 1) {
				space = 0;
			}
			return;
		}
		if (bank.isOpen()) {
			doBank();
			return;
		}
		if (!getMyPlayer().isMoving()) {
			if (!burnLogs(logType)) {
				openBank(banktile, bankOpen);
				return;
			}
		}
	}

	// *******************************************************//
	// OTHER METHODS
	// *******************************************************//
	private void makeFiresFOG(final int bankID, final RSTile[] path,
			final RSTile[] start, final int rand, final String bankOpen) {
		cantPlace();
		if (getNewSpot) {
			ready = false;
			if (!walkTileMM(new RSTile(start[space].getX() - random(0, rand),
					start[space].getY()))) {
				walkPath(path, false);
				return;
			} else {
				wait(random(500, 750));
			}
			getNewSpot = false;
			wait(random(400, 600));
			space++;
			if (space > start.length - 1) {
				space = 0;
			}
			return;
		}
		if (bank.isOpen()) {
			doBank();
			return;
		}
		if (!getMyPlayer().isMoving()) {
			if (!burnLogs(logType)) {
				onNPC(getNearestNPCByID(bankID), bankOpen);
				wait(random(500, 750));
				return;
			}
		}
	}

	public boolean moveToInventoryItem(final int itemID) {
		if (getCurrentTab() != Constants.TAB_INVENTORY
				&& !RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()
				&& !RSInterface.getInterface(Constants.INTERFACE_STORE)
						.isValid()) {
			openTab(Constants.TAB_INVENTORY);
		}
		final int[] items = getInventoryArray();
		final java.util.List<Integer> possible = new ArrayList<Integer>();
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				possible.add(i);
			}
		}
		if (possible.size() == 0) {
			return false;
		}
		final int idx = possible.get(0);
		final Point t = getInventoryItemPoint(idx);
		moveMouse(t, 5, 5);
		return true;
	}

	// *******************************************************//
	// ON FINISH
	// *******************************************************//
	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
		Bot.getEventManager().removeListener(ServerMessageListener.class, this);
	}

	public boolean onNPC(final RSNPC npc, final String action) {
		try {
			if (npc == null) {
				return false;
			}
			final RSTile tile = npc.getLocation();
			final Point screenLoc = Calculations.tileToScreen(tile);
			if (!tile.isValid()) {
				return false;
			}
			if (!pointOnScreen(screenLoc)) {
				if (!walkTileMM(tile) && !getMyPlayer().isMoving()) {
					walkTileMM(thePath[0]);
				}
				return false;
			}
			return clickTheCharacter(npc, action);
		} catch (final Exception e) {
			return false;
		}
	}

	// *******************************************************//
	// PAINT
	// *******************************************************//
	public void onRepaint(final Graphics g) {

		if (openMenu) {
			return;
		}

		long runTime = 0;
		long seconds = 0;
		long minutes = 0;
		long hours = 0;

		runTime = System.currentTimeMillis() - scriptStartTime;
		seconds = runTime / 1000;
		if (seconds >= 60) {
			minutes = seconds / 60;
			seconds -= minutes * 60;
		}
		if (minutes >= 60) {
			hours = minutes / 60;
			minutes -= hours * 60;
		}

		if (getCurrentTab() == Constants.TAB_INVENTORY) {
			g.setColor(new Color(0, 0, 0, 175));
			g.fillRoundRect(555, 210, 175, 250, 10, 10);
			g.setColor(Color.WHITE);

			final int[] coords = new int[] { 225, 240, 255, 270, 285, 300, 315,
					330, 345, 360, 375, 390, 405, 420, 435, 450 };

			g.drawString(getName() + " v" + getVersion(), 561, coords[0]);
			g.drawString("Run Time: " + hours + ":" + minutes + ":" + seconds,
					561, coords[1]);
			g.drawString("Fires Started: " + fireCount, 561, coords[2]);
			g.drawString("Lvls Gained: "
					+ (skills.getCurrentSkillLevel(Skills
							.getStatIndex("firemaking")) - startLvl), 561,
					coords[4]);
			g.drawString("XP Gained: "
					+ (skills.getCurrentSkillExp(Skills
							.getStatIndex("firemaking")) - startFireXP), 561,
					coords[5]);
			g.drawString(
					"XP to Lvl: "
							+ skills.getXPToNextLevel(Skills
									.getStatIndex("firemaking")), 561,
					coords[7]);
			g.drawString("Percent til Lvl: "
					+ skills.getPercentToNextLevel(Skills
							.getStatIndex("firemaking")), 561, coords[8]);
			g.drawString("Wood Type: " + jComboBox1.getSelectedItem(), 561,
					coords[14]);
			g.drawString("Location: " + jComboBox2.getSelectedItem(), 561,
					coords[15]);
		}
	}

	// *******************************************************//
	// ON START
	// *******************************************************//
	@Override
	public boolean onStart(final Map<String, String> args) {

		initComponents();

		while (openMenu) {
			wait(1000);
		}

		scriptStartTime = System.currentTimeMillis();

		HOVER = jCheckBox1.isSelected();

		if (jComboBox1.getSelectedItem() == "Normal") {
			logType = normalLog;
		}
		if (jComboBox1.getSelectedItem() == "Oak") {
			logType = oakLog;
		}
		if (jComboBox1.getSelectedItem() == "Willow") {
			logType = willowLog;
		}
		if (jComboBox1.getSelectedItem() == "Maple") {
			logType = mapleLog;
		}
		if (jComboBox1.getSelectedItem() == "Yew") {
			logType = yewLog;
		}
		if (jComboBox1.getSelectedItem() == "Magic") {
			logType = magicLog;
		}
		if (jComboBox1.getSelectedItem() == "Custom") {
			logType = Integer.parseInt(jTextField1.getText());
		}

		if (jComboBox2.getSelectedItem() == "Fist of Guthix") {
			thePath = FOGPath;
			theStart = FOG;
			randTile = 2;
			openStr = "Bank Banker";
			fog = true;
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Yanille Bank") {
			thePath = YanillePath;
			theBank = YanilleBank;
			theStart = Yanille;
			randTile = 4;
			openStr = "Use-quickly";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Duel Arena Bank") {
			thePath = DuelArenaPath;
			theBank = DuelArenaBank;
			theStart = DuelArena;
			randTile = 4;
			openStr = "Bank Bank";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Falador East Bank") {
			thePath = FaladorPath;
			theBank = FaladorBank;
			theStart = Falador;
			randTile = 4;
			openStr = "Use-quickly";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Draynor Bank") {
			thePath = DraynorPath;
			theBank = DraynorBank;
			theStart = Draynor;
			randTile = 4;
			openStr = "Use-quickly";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Grand Exchange 1") {
			thePath = GE1Path;
			theBank = GEBank1;
			theStart = GE1;
			randTile = 2;
			openStr = "Bank Banker";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Grand Exchange 2") {
			thePath = GE2Path;
			theBank = GEBank2;
			theStart = GE2;
			randTile = 3;
			openStr = "Bank Banker";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Grand Exchange 3") {
			thePath = GE3Path;
			theBank = GEBank3;
			theStart = GE3;
			randTile = 3;
			openStr = "Bank Banker";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Varrock West Bank (North)") {
			thePath = VarrockNorthPath;
			theBank = VarrockNorthBank;
			theStart = VarrockNorth;
			randTile = 4;
			openStr = "Use-quickly";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Varrock West Bank (South)") {
			thePath = VarrockPath;
			theBank = VarrockBank;
			theStart = Varrock;
			randTile = 4;
			openStr = "Use-quickly";
			return true;
		}
		if (jComboBox2.getSelectedItem() == "Varrock East Bank") {
			thePath = VarrockEastPath;
			theBank = VarrockEastBank;
			theStart = VarrockEast;
			randTile = 4;
			openStr = "Use-quickly";
			return true;
		}
		return false;
	}

	private boolean onTile(final RSTile tile, final String action) {
		try {
			final Point location = Calculations.tileToScreen(tile);
			if (location.x == -1 || location.y == -1) {
				return false;
			}
			moveMouse(location, 3, 3);
			wait(random(250, 500));
			if (getMenuItems().get(0).toLowerCase().contains(
					action.toLowerCase())) {
				clickMouse(true);
				wait(random(250, 500));
			} else {
				clickMouse(false);
				if (!atMenu(action)) {
					return false;
				}
			}
			wait(random(200, 400));
			while (true) {
				if (!getMyPlayer().isMoving()) {
					break;
				}
				wait(random(100, 200));
			}
			return true;
		} catch (final Exception e) {
			return false;
		}
	}

	private void openBank(final RSTile[] loc, final String text) {
		final int randomTile = random(0, loc.length);
		final Point location = Calculations.tileToScreen(loc[randomTile]);
		if (pointOnScreen(location)) {
			if (!getMyPlayer().isMoving()) {
				onTile(loc[randomTile], text);
				wait(random(400, 600));
				return;
			} else {
				antiBan();
			}
		} else {
			if (!getMyPlayer().isMoving()) {
				if (!walkTileMM(loc[randomTile])) {
					walkPath(thePath, false);
				}
				return;
			} else {
				antiBan();
			}
		}
	}

	public boolean playerInArea(final int maxX, final int maxY, final int minX,
			final int minY) {
		final int x = getMyPlayer().getLocation().getX();
		final int y = getMyPlayer().getLocation().getY();
		if (x >= minX && x <= maxX && y >= minY && y <= maxY) {
			return true;
		}
		return false;
	}

	// *******************************************************//
	// MESSAGE READER
	// *******************************************************//
	public void serverMessageRecieved(final ServerMessageEvent e) {
		final String messageEvent = e.getMessage();
		if (messageEvent == lastMessage
				&& !messageEvent.contains("you are dead")) {
			return;
		}
		lastMessage = e.getMessage();
		checkMessage = e.getMessage();
	}

	private void walkPath(final RSTile[] path, final boolean reverse) {
		if (!reverse) {
			if (!getMyPlayer().isMoving()
					|| distanceTo(getDestination()) <= random(4, 7)) {
				walkPathMM(randomizePath(path, 2, 2), 17);
				return;
			} else {
				antiBan();
			}
		} else {
			if (!getMyPlayer().isMoving()
					|| distanceTo(getDestination()) <= random(4, 7)) {
				walkPathMM(randomizePath(reversePath(path), 2, 2), 17);
				return;
			} else {
				antiBan();
			}
		}
	}
}